package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import sjtu.ist.warehouse4ws.model.User;
import sjtu.ist.warehouse4ws.service.UserService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private List<User> userList = new ArrayList<User>();
	private UserService userService;
	private User user;
	private boolean success;
	private String message;
	
	private String userName;
	private String pw1;
	private String pw2;
	
	private String id;
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	
	
	@JSON(name = "totalUser")
	public int getTotalUserList() {
		return this.userList.size();
	}
	
	@JSON(name = "userList")
	public List<User> getUserList() {
		return userList;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@JSON(name = "message")
	public String getMessage() {
		return message;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

//	@JSON(name = "isUserNameAdd")
//	public boolean isUserNameAdd() {
//		return userService.isUserNameAdd(userName, password);
//	}

	
	
	
	//@JSON(name = "companyUser")
	public String showUserList(){
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();
		
		String userCompany=session.get("user_company").toString();
		this.userList = userService.getUserList(userCompany,2);
		success=true;
		return SUCCESS;
	}
	
	public String addCompanyUser(){
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();
		
		String userCompany=session.get("user_company").toString();
		
		String str1="用户名已经被添加，请重新选择用户名";
		String str2="用户添加成功";
		setSuccess(true);
		if(userService.isUserNameAdd(getUserName(), getPw1())){
			setMessage(str1);
			return SUCCESS;
		}
		user = new User();
		String userId = java.util.UUID.randomUUID().toString();
		user.setId(userId);
		user.setName(userName);
		user.setPassword(pw1);
		user.setType(2);
		user.setCompany(userCompany);
		userService.save(user);
		setMessage(str2);
		return SUCCESS;
	}

	public String deleteCompanyUser(){
	
		userService.delete(id);
		return SUCCESS;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getPw2() {
		return pw2;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
	
	

}
